package com.mobilesales.android.services;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import com.google.gson.reflect.TypeToken;
import com.mobilesales.android.model.clients.Client;
import com.mobilesales.android.model.clients.ClientGroup;
import com.mobilesales.android.model.exceptions.ServiceUnavailableException;

public class ClientServiceClient extends ServiceClientBase {

	public ClientServiceClient(Configuration configuration) {
		super(configuration);
		// TODO Auto-generated constructor stub
	}

	public Client getClient(int clientId) throws ServiceUnavailableException {
		Client client = null;
		String url = getMethodURL() + "/" + String.valueOf(clientId);
		client = (Client) get(url, Client.class);

		return client;
	}

	public List<Client> getClientList() throws ServiceUnavailableException {
		List<Client> clientList = new ArrayList<Client>();
		List<Object> objectList = null;
		Type listType = new TypeToken<ArrayList<Client>>(){}.getType();
		String url = getMethodURL();
		objectList = getList(url, listType);
		if (objectList != null){
			for(Object obj : objectList){
				clientList.add((Client)obj);
			}
		} else 
			return null;
		return clientList;
	}

	public void storeClient(Client client) throws ServiceUnavailableException {
		String url = getMethodURL();
		post(url, client);
	}

	public ClientGroup getClientGroup(int clientGroupId) throws ServiceUnavailableException{
		ClientGroup clientGroup = null;
		String url = getMethodURL() + "/" + String.valueOf(clientGroupId);
		clientGroup = (ClientGroup) get(url, ClientGroup.class);
		return clientGroup;
	}
	
	public List<ClientGroup> getClientGroupList() throws ServiceUnavailableException{
		List<ClientGroup> clientGroupList = new ArrayList<ClientGroup>();
		List<Object> objectList = null;
		Type listType = new TypeToken<ArrayList<ClientGroup>>(){}.getType();
		String url = getMethodURL();
		objectList = getList(url, listType);
		if (objectList != null){
			for(Object obj : objectList){
				clientGroupList.add((ClientGroup)obj);
			}
		} else 
			return null;
		return clientGroupList;
	}
}
